Skip to content

client: reuse one no-redirect opener for calls without an SSL context (#722) - #807

Merged
LoopGlitch26 merged 1 commit into
Cygnux-Labs:mainfrom
khushi-git-7:agent/issue-722-opener
Nov 13, 2026
Merged

LoopGlitch26 merged 1 commit into
Cygnux-Labs:mainfrom
khushi-git-7:agent/issue-722-opener

Conversation

@khushi-git-7

@khushi-git-7 khushi-git-7 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Part (3) of #722, step (2), as agreed there: one opener for every call that brings no SSL context.

Why

no_redirect_opener() built a new opener on every call. On Python 3.12 and later, urllib.request.HTTPSHandler() creates its SSL context when it's built, and build_opener adds one even for http:// URLs. On Windows, that loads the system certificate store each time. In the e20 test's profile, it cost ~57 s across ~1,100 calls, about 40 ms a call. The webhook, witness, Slack, Rekor, OTLP, reviewer and remote-ingest senders all paid it per request.

Change (tracekit/client.py)

  • no_redirect_opener(context=None) returns _shared_opener(), built once (functools.lru_cache(maxsize=1)). A caller that passes its own context (executor_tools) still gets a new opener of its own.
  • The opener is built exactly as before (_no_redirect_opener): ProxyHandler({}), the same no-redirect handler, and the TLS handler only for a given context.

Thread safety, what I checked (CPython 3.14's urllib.request source; the CI runs above cover 3.9 and 3.13): OpenerDirector.open, _open and _call_chain don't assign to self. The handler tables are filled only by add_handler, which build_opener calls once. HTTPRedirectHandler.http_error_302 keeps its loop state on the request (req.redirect_dict), and ours returns None before that. AbstractHTTPHandler.do_open makes a new connection per request. ProxyHandler only reads self.proxies. HTTPSHandler holds its _context, and an SSLContext is safe to share across threads. The one lazily filled cache, FileHandler.names, is class-level already, so sharing changes nothing there.

Tests (tests/test_ingest.py)

  • test_calls_without_a_context_share_one_opener_and_its_ssl_context: two calls return the same opener and create at most one SSL context (one on 3.12+, none before, where each HTTPS connection makes its own); a caller's own context gets a different opener.
  • test_redirect_is_not_followed now sends its request twice, so the second one goes through the shared opener. Both are refused, and the redirect target is never contacted.

Checked on my fork

…Cygnux-Labs#722)

no_redirect_opener() built a new opener per call, and building one makes an
SSL context (Python 3.12 on), which on Windows loads the system's
certificates each time: ~40 ms a call for every webhook, witness, Slack,
Rekor, OTLP, reviewer and remote-ingest request. A call without a context now
gets the same opener; a caller's own context still gets its own. The
ProxyHandler({}) and the no-redirect handler are unchanged, and neither the
OpenerDirector nor its handlers keep per-request state, so the opener is
shared across threads.

Tests: two calls share one opener and make at most one SSL context, and a
redirect is still refused on the second call, through the shared opener.

Signed-off-by: Khushi Jain <jainkhushibts@gmail.com>
@LoopGlitch26

Copy link
Copy Markdown
Member

Read it. This is what we agreed:

  • the opener is shared only for context=None, built once;
  • the thread-safety reasoning is in the docstring;
  • the redirect-refusal test now runs a second call through the shared opener;
  • one SSL context across two calls, while a caller's own context keeps its own opener.

ProxyHandler({}) and the no-redirect handler are unchanged. It goes on the merge list once CI is green.

@LoopGlitch26
LoopGlitch26 enabled auto-merge (squash) November 13, 2026 10:58
@LoopGlitch26
LoopGlitch26 merged commit 7ed8145 into Cygnux-Labs:main Nov 13, 2026
32 of 34 checks passed
@LoopGlitch26

Copy link
Copy Markdown
Member

Merged, thank you. One shared opener for calls without an SSL context removes the per-call setup cost you measured on Windows, and both of your #722 fixes are now on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants